package com.bsoft.myapplication;

import android.accessibilityservice.AccessibilityService;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * User: ZRJ
 * Date: 2017/4/5
 * Email: 471564517@qq.com
 * Description:
 */
public class RobService extends AccessibilityService {
    private String TAG = getClass().getSimpleName();

    private boolean isFinish = false;

    public static RobService instance;
    private int index = 1;

    /**
     * 获取到短信通知
     * 0.唤醒屏幕
     * 1.打开钉钉
     * 2.确保当前页是主页界面
     * 3.找到“工作”tab并且点击
     * 4.确保到达签到页面
     * 5.找到签到按钮，并且点击
     * 6.判断签到是否成功
     * 1.成功，退出程序
     * 2.失败，返回到主页，重新从1开始签到
     */


    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        // TODO Auto-generated method stub
        Log.i(TAG, "事件---->" + event.getEventType());
        if (isFinish) {
            back();
            return;
        }

        AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
        if (nodeInfo == null) {
            Log.w(TAG, "rootWindow为空");
            return;
        }
        System.out.println("index:" + index);
        switch (index) {

            case 1: //进入主页
                openPacket(event.getEventType(), nodeInfo);
                click("签到");
                index = 2;
                break;
            case 2: //进入签到页
                List<AccessibilityNodeInfo> list1 = nodeInfo.findAccessibilityNodeInfosByViewId("com.alibaba.android.rimet:id/common_webview");
                AccessibilityNodeInfo webView = list1.get(0).getChild(0).getChild(0).getChild(0).getChild(4).getChild(0);
                onclick(webView);
                break;

            case 3:
                List<AccessibilityNodeInfo> list3 = nodeInfo.findAccessibilityNodeInfosByViewId("com.alibaba.android.rimet:id/common_webview");
                //提交
                AccessibilityNodeInfo tijiao = list3.get(0).getChild(0).getChild(0).getChild(0).getChild(5);
                onclick1(tijiao);
                break;

            default:
                break;
        }

    }

    private void inputEdt(AccessibilityNodeInfo edtdesc) {
        Bundle arguments = new Bundle();
        arguments.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE,"天津分公司");
        edtdesc.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);
    }


    private ArrayList<String> getTextList(AccessibilityNodeInfo node, ArrayList<String> textList) {
        if (node == null) {
            Log.w(TAG, "rootWindow为空吗");
            return null;
        }
        if (textList == null) {
            textList = new ArrayList<String>();
        }
        String text = node.getText().toString();
        if (text != null && text.equals("")) {
            textList.add(text);
        }
//        node.get
        return null;

    }
    //通过ID进行点击
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    private void openPacket(int type, AccessibilityNodeInfo nodeInfo) {
        if (type == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {

            if (nodeInfo != null) {
                List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByViewId("com.alibaba.android.rimet:id/home_bottom_tab_button_work");
                nodeInfo.recycle();
                for (AccessibilityNodeInfo item : list) {
                    item.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                }
            }
        }
    }

    /**
     * 模拟点击,打开抢红包界面
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void getPacket() {
        AccessibilityNodeInfo rootNode = getRootInActiveWindow();
        AccessibilityNodeInfo node = recycle(rootNode);

        node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
        AccessibilityNodeInfo parent = node.getParent();
        while (parent != null) {
            if (parent.isClickable()) {
                parent.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                break;
            }
            parent = parent.getParent();
        }

    }

    public AccessibilityNodeInfo recycle( AccessibilityNodeInfo node) {
            if (node.getChildCount() == 0) {
                if (node.getText() != null) {
                    if ("签到".equals(node.getText().toString())) {
                        return node;
                    }
                }
            } else {
                for (int i = 0; i < node.getChildCount(); i++) {
                    if (node.getChild(i) != null) {
                        recycle(node.getChild(i));
                    }
                }
            }


        return node;
    }
    //通过文字点击
    private boolean click(String viewText) {
        AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
        if (nodeInfo == null) {
            Log.w(TAG, "点击失败，rootWindow为空");
            return false;
        }
        List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByText(viewText);
        if (list.isEmpty()) {
            //没有该文字的控件
            Log.w(TAG, "点击失败，" + viewText + "控件列表为空");
            return false;
        } else {
            //有该控件
            //找到可点击的父控件
            AccessibilityNodeInfo view = list.get(0);
            return onclick(view);  //遍历点击
        }

    }

    private boolean onclick1(AccessibilityNodeInfo view) {
        if (view.isClickable()) {
            view.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            Log.w(TAG, "点击成功");

            isFinish = true;

            return true;
        } else {

            AccessibilityNodeInfo parent = view.getParent();
            if (parent == null) {
                return false;
            }
            onclick1(parent);
        }
        return false;
    }




    private boolean onclick(AccessibilityNodeInfo view) {
        if (view.isClickable()) {
            view.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            index = 3;
            Log.w(TAG, "点击成功");
            return true;
        } else {

            AccessibilityNodeInfo parent = view.getParent();
            if (parent == null) {
                return false;
            }
            onclick(parent);
        }
        return false;
    }

    //点击返回按钮事件
    private void back() {
        performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
    }

    @Override
    public void onInterrupt() {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onServiceConnected() {
        // TODO Auto-generated method stub
        super.onServiceConnected();
        Log.i(TAG, "service connected!");
        Toast.makeText(getApplicationContext(), "连接成功！", Toast.LENGTH_SHORT).show();
        instance = this;
    }

    public void setServiceEnable() {
        isFinish = false;
        Toast.makeText(getApplicationContext(), "服务可用开启！", Toast.LENGTH_SHORT).show();
        index = 1;
    }

}
